package com.gxz.config;

import com.gxz.controller.interceptor.Interceptor2;
import com.gxz.controller.interceptor.ProjectInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan("com.gxz.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(new ProjectInterceptor()).addPathPatterns("/users/**");
       registry.addInterceptor(new Interceptor2()).addPathPatterns("/users/**");
    }
    /*
    * 1.拦截去和过滤器区别
    * 拦截器是在controller之前执行，java是基于反射机制，拦截器适用于日志记录、权限检查、性能监控、通用行为处理等
    * 过滤器是在进入setvlet 之前调用，过滤器是基于函数调用，依赖servlet容器执行，过滤器则适用于请求预处理，如字符集设置、非法字符过滤等
    * 2.拦截器执行顺序
    * pre1--pre2--pre3--controller--post3--post2--post1--after3--after2--after1
    * 假设pre2 return false, 跳过controller和after, 执行顺序为 pre1--pre2--after1
    * */
}
